package com.atguigu.boot.admin.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 *   文件上传测试
 *
 *
 * @author : leimingchao
 * @date Date : 2021年11月20日 9:27
 */
@Controller
@Slf4j
public class FormTestController {

    @GetMapping(value = "/form_layouts")
    public String form_layouts() {

        return "form/form_layouts";
    }

    /**
     * MultipartFile自动封装上传过来的文件
     *      <input type="file" name="headerImg" id="exampleInputFile">
     *          根据文件项表单name获取MultipartFile：MultipartHttpServletRequest.getFile(String name)
     *
     *      <input type="file" name="photos" multiple>
     *          根据文件项表单name获取MultipartFile[]：MultipartHttpServletRequest.getFiles(String name)
     *          List<MultipartFile> files = MultipartHttpServletRequest.getFiles();
     *
     * @param email
     * @param username
     * @param headerImg
     * @param photos
     * @return
     */
    @PostMapping(value = "/upload")
    public String upload(@RequestParam(value = "email") String email,
                         @RequestParam(value = "username",required = false) String username,
                         @RequestPart(value = "headerImg") MultipartFile headerImg,
                         @RequestPart(value = "photos") MultipartFile[] photos) throws IOException {
        log.info("上传的信息：email={}，username={}，headerImg={}，photos={}",email,username,headerImg.getSize(),photos.length);
        if (!headerImg.isEmpty()) {
            log.info("headerImg文件项名字：{}",headerImg.getName());
            String originalFilename = headerImg.getOriginalFilename();
            Path path = Paths.get("D:\\io", originalFilename);
            boolean exists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
            if (!exists) {
                //保存到本地服务器，OSS服务器
                headerImg.transferTo(Files.createFile(Paths.get("D:\\io",originalFilename)));
            }
        }

        if (photos.length > 0) {
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()) {
                    log.info("photos文件项名字：{}",photo.getName());
                    String originalFilename = photo.getOriginalFilename();
                    //Path createFile(Path path, FileAttribute<?> … arr) : 创建一个文件
                    //要想执行成功，要求path对应的物理上的文件不存在。一旦存在，抛出异常。
                    Path path = Paths.get("D:\\io", originalFilename);
                    boolean exists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
                    if (!exists) {
                        photo.transferTo(Files.createFile(path));
                    }
                }
            }
        }

        return "main";
    }
}
